package com.wanlian.candy.controller;

import com.wanlian.candy.controller.vo.MsgSendVo;
import com.wanlian.entity.UserPhoneCode;
import com.wanlian.error.ErrorCode;
import com.wanlian.jpush.service.YunPianSmsServices;
import com.wanlian.json.JsonResult;
import com.wanlian.json.JsonStringUtil;
import com.wanlian.model.ResultModel;
import com.wanlian.service.UserServices;
import com.wanlian.util.Utils;
import com.wanlian.vo.ReturnMsgCodeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/valitation")
public class ValitationController {

    @Autowired
    private YunPianSmsServices yunPianSmsServices;

    @Autowired
    private UserServices userService;


    /**
     * 发送短信验证码
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/msg/code", method = {RequestMethod.POST})
    @ResponseBody
    public String sendMessage(@RequestBody String data) {
        ResultModel resultModel = new ResultModel();
        String code = Utils.getRandomNumCode(6);
        try {
            MsgSendVo inputVo = JsonStringUtil.readValue(data, MsgSendVo.class);
            ReturnMsgCodeVo msgReturnVo = yunPianSmsServices.sendSmsCodeSing(inputVo.getMobile(), code);
            resultModel.setBresult(msgReturnVo.checkSuccess());
            if (msgReturnVo.checkSuccess()) {
                UserPhoneCode userPhoneCode=new UserPhoneCode();
                userPhoneCode.setCode(code);
                userPhoneCode.setPhone(inputVo.getMobile());
                userService.doAddPhoneCode(userPhoneCode);
            }
            resultModel.setMessage(msgReturnVo.getMsg());
        } catch (Exception e) {
            resultModel.setBresult(false);
            resultModel.setMessage(ErrorCode.CODE_DEFAULT_NOSEND);
        }
        return new JsonResult().Serailze(resultModel);
    }


    /**
     * 发送短信验证码(form)
     *
     * @return
     */
    @RequestMapping(value = "/msg/form/code/{phone}")
    @ResponseBody
    public String sendMessage(@PathVariable("phone") String phone, HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "*");
        response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setContentType("application/json");
        ResultModel resultModel = new ResultModel();
        String code = Utils.getRandomNumCode(6);
        try {
            ReturnMsgCodeVo msgReturnVo = yunPianSmsServices.sendSmsCodeSing(phone, code);
            resultModel.setBresult(msgReturnVo.checkSuccess());
            if (msgReturnVo.checkSuccess()) {
                UserPhoneCode userPhoneCode=new UserPhoneCode();
                userPhoneCode.setCode(code);
                userPhoneCode.setPhone(phone);
                userService.doAddPhoneCode(userPhoneCode);
            }
            resultModel.setMessage(msgReturnVo.getMsg());
        } catch (Exception e) {
            resultModel.setBresult(false);
            resultModel.setMessage(ErrorCode.CODE_DEFAULT_NOSEND);
        }
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 验证短信验证码
     *
     * @param data
     * @param session
     * @return
     */
    @RequestMapping(value = "/msg/code/check", method = {RequestMethod.POST})
    @ResponseBody
    public String checkMessageCode(@RequestBody String data, HttpSession session) {
        MsgSendVo inputVo = JsonStringUtil.readValue(data, MsgSendVo.class);
        String mobile = inputVo.getMobile();
        ResultModel resultModel = userService.queryPhoneCodeByPhone(mobile);
        if(resultModel.isBresult()){
            UserPhoneCode code = (UserPhoneCode) resultModel.getObject();
            if (code.getCode().equals(inputVo.getCode())) {
                resultModel.setBresult(true);
            } else {
                resultModel.setBresult(false);
                resultModel.setMessage(ErrorCode.CODE_DEFAULT);
            }
        }
        return new JsonResult().Serailze(resultModel);
    }
}
